<?php
include("config.php");
$conn=@mysql_connect($host,$user,$password) or die("Connect to database ... failed");
mysql_query("SET NAMES UTF8");
mysql_select_db($db) or die("Open database ... failed");
$query="select * from vote_title";
$result=mysql_query($query);
if(mysql_num_rows($result)==0)
{
	echo "<script language='javascript'>alert('There is no topic yet!');window.close();</script>";
	exit();
}
$ip=$_SERVER['REMOTE_ADDR'];
$arr_ip=explode(".",$ip);
$str_ip="";
for($i=0;$i<count($arr_ip);$i++)
{
	$str_ip.=$arr_ip[$i];
}
$sql_ip="select * from vote_ip where ip='$str_ip'";
$query_ip=mysql_query($sql_ip);
if($_POST){

	/*
	if(mysql_num_rows($query_ip))
	{
		
		echo "<script language='javascript'>alert('One vote per one IP address!');window.location.href='index.php';</script>";
		exit();
	
	}
	else
	{
	*/
		while($row3=mysql_fetch_assoc($result))
		{
			if($_POST["choice_".$row3[id]]==null && count($_POST["choice_".$row3[id]."_"])==0) 
			{
				echo "<script language='javascript'>alert('There are unchoiced options!');window.location.href='index.php';</script>";
				exit();				 
			}
		}
		foreach($_POST as $key=>$value)
		{
			$arr=explode("_",$key);
			if(!is_array($arr)) 
				continue;
			$titleid=$arr[1];
			$id=$value;
			$result=mysql_query("select * from vote_title where id='$titleid'");
			$row=mysql_fetch_assoc($result);
			if($row['options']=='a')
			{
				$query="update vote_choice set num=num+1 where id='$id'";
				$result1=mysql_query($query);
			}
			else if($row['options']=='b')
			{
				for($i=0;$i<count($_POST[$key]);$i++)
				{   
					$query="update vote_choice set num=num+1 where id=".$_POST[$key][$i];
					$result=mysql_query($query);
				}
			}
		}
		mysql_query("insert into vote_ip(ip) values ('$str_ip')");
		if(!empty($_POST[opinion]))
		{
			if(strlen($_POST[opinion])<=200)
			{
				$opinion=$_POST[opinion];
				mysql_query("insert into vote_opinion(opinion) values('$opinion')");
			}
		}
		echo "<script language='javascript'>alert('Test finished, Thanks for your help!');window.location.href='index.php';</script>";
	/*
	}
	*/
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Questionnaire Survey System --- TMM 2012</title>
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="info1">
<br />
<h3><center>Subjective Questionaire Survey</center></h3>
<!--Write the instruction information here-->
This whole form is designed for the Subjective test of TMM project. You will be shown sets of videos, when each video is finished, please rate the quality from 1 to 5.
5 is the best, while 1 is the worst. Thanks for your time.

</div>
<div id="vote">
<form name="form1" method="post" action="">
<?php 
while($row=mysql_fetch_array($result))
{
?>
	<div class="singlevote">
		<div class="votetitle"><?php echo $row["title"];?></div>
		<div class="votecontent">
		<?php
			$query1="select * from vote_choice where titleid=".$row['id'];
			$result1=mysql_query($query1);
			if (mysql_num_rows($result1)==0)
			{
				echo "<script language='javascript'>alert('Please add the vote options first!');window.close();</script>";
				exit();
			}
			else
			{
				while($row1=mysql_fetch_array($result1))
				{
					if ($row1["IsOptions"]=="a")
					{
						$de="checked";
					}
					else
					{
						$de="";
					}

					if ($row["options"]=="a")
						echo "<div class='votelist'><input name=choice_".$row1['titleid']." type=radio value=".$row1["id"]." ".$de.">".$row1["choice"]."</div>\n";
					elseif ($row["options"]=="b")
						echo "<div class='votelist'><input name=choice_".$row1['titleid']."_[] type=checkbox value=".$row1["id"]." ".$de.">".$row1["choice"]."</div>\n";
				}	
			}
		?> 
		</div>
	</div>
<?php 
} 
?> 
		
<div class="submit">
	<div class="votelist"><input name="type" type="hidden" id="type" value="1"><input type="image" src="button1.gif" width="50" height="20"></a></div>
	</div>

</form>
</div>
		
<div class="info2">
	<!-- information block again, you can write some information here, or nothing-->
	Yes, Job Done, Thanks again!
</div>
<br />
		
<center>CopyRight@FengYL <a href="login.php" target="_blank">Management</a></center>
</body>
</html>
